package com.tiandao.mofang.core.jingji;

import com.tiandao.mofang.core.RobotSingleton;
import com.tiandao.mofang.ui.MFFrame;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Date;

import static com.tiandao.mofang.core.jingji.BaseJingJi.OpenJieMian;

/**
 * 行为模仿论剑
 */
public class LuJian implements Runnable {
    public static int HAO_NUM = 1;
    private static Robot robot = RobotSingleton.getInstance();
    public static int flag = 1;
    @Override
    public void run() {
        LuJian.OpenLunJian();
    }

    public static void main(String[] args) {
//        robot.mouseMove(792,303);
        OpenLunJian();
    }
    /**
     * 论剑排队操作
     */
    public static void OpenLunJian(){
        int i = 1;
        while(i<=HAO_NUM){
            if(i==1){
                MFFrame.textArea.append(new Date()+":    第一个号开始!\n");
                start();
                MFFrame.textArea.append(new Date()+":    第一个号结束!\n");
            }
            if(i==2){
                try {
                    Thread.sleep(3000);
                    robot.keyPress(KeyEvent.VK_ALT);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyRelease(KeyEvent.VK_ALT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MFFrame.textArea.append(new Date()+":    第二个号开始!\n");
                start();
                MFFrame.textArea.append(new Date()+":    第二个号结束!\n");
            }
            if(i==3){
                try {
                    Thread.sleep(3000);
                    robot.keyPress(KeyEvent.VK_ALT);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyRelease(KeyEvent.VK_ALT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MFFrame.textArea.append(new Date()+":    第三个号开始!\n");
                start();
                MFFrame.textArea.append(new Date()+":    第三个号结束!\n");
            }
            if(i==4){
                try {
                    Thread.sleep(3000);
                    robot.keyPress(KeyEvent.VK_ALT);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyRelease(KeyEvent.VK_ALT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MFFrame.textArea.append(new Date()+":    第四个号开始!\n");
                start();
                MFFrame.textArea.append(new Date()+":    第四个号结束!\n");
            }
            if(i==5){
                try {
                    Thread.sleep(3000);
                    robot.keyPress(KeyEvent.VK_ALT);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
                    Thread.sleep(200);
                    robot.keyRelease(KeyEvent.VK_ALT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MFFrame.textArea.append(new Date()+":    第五个号开始!\n");
                start();
                MFFrame.textArea.append(new Date()+":    第五个号结束!\n");
            }
            i++;
        }

    }

    public static void start(){
        int x = 1;
        while (x<=5){
            OpenJieMian();
            try {
                Thread.sleep(1500);
                robot.mouseMove(792,303);
                Thread.sleep(1001);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                //点击排队
                Thread.sleep(1000);
                robot.mouseMove(1000,710);
                Thread.sleep(1000);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                //监听是否进入位面
                LuJian.startListener();
                //调用进入位面的细分方法
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            x++;
        }
    }
    /**
     * 监听是否进入
     */
    public static boolean listener(){
        //每1.5秒截图判断一次
        BufferedImage img = robot.createScreenCapture(new Rectangle(100, 10, 5, 5));
        int[] rgbS = img.getRGB(0,0,5,5,null,0,5);
        int r = 0;
        int g = 0;
        int b = 0;
        //黑色系计数
        int weiNum = 0;
        //循环判断所有像素点
        for(int i = 0;i<rgbS.length;i++){
            Color myColor = new Color(rgbS[i]);
            r = myColor.getRed();
            g = myColor.getGreen();
            b = myColor.getBlue();
            if(r<=11&&g<=11&&b<=11){
                weiNum++;
            }
            if(weiNum>10){
                break;
            }
        }
        if(weiNum >=10){
            return true;
        }
        return false;
    }

    //论剑战斗要像模像样 职业细分 10个
    public static void startListener(){
        MFFrame.textArea.append(new Date()+":    开启监听论剑开始\n");
        System.out.println("开启监听论剑开始!\n");
        for (int i = 0;i<140;i++){
            long a = System.currentTimeMillis();
            try {
                Thread.sleep(1000);
                if(listener()){
                    MFFrame.textArea.append(new Date()+":    监听到进入论剑位面!\n");
                    System.out.println("监听到进入论剑位面!\n");
                    Fight();
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("监听器(开始)执行时间: " + (System.currentTimeMillis() - a) );
        }
    }

    public static void Fight(){
        try {
            Thread.sleep(35000);
            //点击准备
            //移动鼠标到点击准备上并点击
            robot.mouseMove(960,220);
            Thread.sleep(1000);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            Thread.sleep(5000);

            //监听是否结束
            new Thread(new LuJianStopListenerThread()).start();
            MFFrame.textArea.append(new Date()+":    开启监听 论剑结束\n");
            System.out.println("开启监听 论剑结束!\n");
            flag = 1;
            for(int i = 1;i<1000;i++){
                if(flag!=1){
                    break;
                }
                //打
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_1);
                robot.keyRelease(KeyEvent.VK_1);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_2);
                robot.keyRelease(KeyEvent.VK_2);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_3);
                robot.keyRelease(KeyEvent.VK_3);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_4);
                robot.keyRelease(KeyEvent.VK_4);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_Q);
                robot.keyRelease(KeyEvent.VK_Q);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_E);
                robot.keyRelease(KeyEvent.VK_E);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_R);
                robot.keyRelease(KeyEvent.VK_R);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_T);
                robot.keyRelease(KeyEvent.VK_T);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_G);
                robot.keyRelease(KeyEvent.VK_G);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_9);
                robot.keyRelease(KeyEvent.VK_9);
                Thread.sleep(900);
                robot.keyPress(KeyEvent.VK_0);
                robot.keyRelease(KeyEvent.VK_0);
            }
            Thread.sleep(2000);
            robot.keyPress(KeyEvent.VK_7);
            robot.keyRelease(KeyEvent.VK_7);
            Thread.sleep(18000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
